//
//  NetWorkNoticeView.m
//  StylisticCircle
//
//  Created by wujian on 2018/7/26.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "NetWorkNoticeView.h"

static NetWorkNoticeView *netWorkView = nil;

@implementation NetWorkNoticeView

+ (instancetype)shareInstance{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        netWorkView = [[NetWorkNoticeView alloc] init];
    });
    
    netWorkView.noticeLabel.textColor = CSWhiteColor;
    
    return netWorkView;
}

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = BackGroundColor;
        
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"icon_Networkanomalies"];
        [self addSubview:imageView];
        self.imageView = imageView;
        
        /**
         提示Label
         */
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"网络异常";
        titleLabel.font = BoldMidBigFont;
        titleLabel.textColor = CSWhiteColor;
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        /**
         提示Label
         */
        UILabel *noticeLabel = [[UILabel alloc] init];
        noticeLabel.text = @"网络异常，请检查网络";
        noticeLabel.font = BoldMidBigFont;
        noticeLabel.textColor = CSWhiteColor;
        [self addSubview:noticeLabel];
        self.noticeLabel = noticeLabel;
        
        /**
         提交按钮
         */
        UIButton *commitBtn = [[UIButton alloc] init];
        [commitBtn setTitleColor:CSColorFromRGB(0x71601D)];
        [commitBtn setTitle:@"刷新网络"];
        commitBtn.layer.cornerRadius = 20;
        commitBtn.layer.masksToBounds = YES;
        [self addSubview:commitBtn];
        self.commitBtn = commitBtn;
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.imageView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self);
        
        make.bottom.mas_equalTo(self.mas_centerY).offset(-10);
    }];
    
    /**
     提示Label
     */
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self);
        
        make.top.mas_equalTo(self.imageView.mas_bottom).offset(20);
    }];
    
    /**
     提示Label
     */
    [self.noticeLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self);
        
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(15);
    }];
    
    /**
     提交按钮
     */
    [self.commitBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.noticeLabel.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
        make.width.mas_equalTo(150);
    }];
    
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.commitBtn.bounds;  // 设置显示的frame
    gradientLayer.colors = @[(id)CSColorFromRGB(0xFFEC00).CGColor,(id)CSColorFromRGB(0xFECD00).CGColor];  // 设置渐变颜色
    gradientLayer.startPoint = CGPointMake(0, 0);   //
    gradientLayer.endPoint = CGPointMake(1, 0);     //
    [self.commitBtn.layer insertSublayer:gradientLayer atIndex:0];
}


/**
 显示View
 */
- (void)showInView:(UIView *)view{
    
    [view insertSubview:self atIndex:0];
    
    [view bringSubviewToFront:self];
    
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(view);
    }];
}

- (void)showTopInView:(UIView *)view{
    
    [view insertSubview:self atIndex:0];
    
    [self mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(view);
        
        make.centerY.mas_equalTo(view).offset(-50);
    }];
}

- (void)hideInView:(UIView *)view{
    
    [self removeFromSuperview];
}

@end
